package cn.gok.homework;


import java.io.*;
import java.util.Comparator;
import java.util.TreeSet;

public class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }

    private static void writeToFile(School s1, School s2) throws IOException, ClassNotFoundException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day5/1.dat"));
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day5/1.dat"));
        oos.writeObject(s1);
        oos.writeObject(s2);
        oos.close();
        try {
            while (true) {
                Object obj = ois.readObject(); //作为一般对象来读取。
                System.out.println(obj);
            }
        } catch (EOFException e) {  //通过捕获文件末尾异常，跳出死循环
            System.out.println("--------------------");
        }
        ois.close();

    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        School s1 = new School("泉州职业技术大学", "晋江", 1988);
        School s2 = new School("泉州理工职业学院", "泉州", 1977);
        writeToFile(s1, s2);
        常用方法();
        自定义比较规则();

    }

    private static void 自定义比较规则() {
        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student(1, "a", 0));
        set.add(new Student(1, "b", 5));
        set.add(new Student(2, "b", 7));
        set.add(new Student(2, "c", 3));
        set.add(new Student(3, "a", 2));
        set.stream().forEach(System.out::println);
        System.out.println("--------------------");
    }

    private static void 常用方法() {
        TreeSet<Student> set = new TreeSet<>(new MyStudentComparator());
        set.add(new Student(1, "a", 0));
        set.add(new Student(1, "b", 5));
        set.add(new Student(2, "b", 7));
        set.add(new Student(2, "c", 3));
        set.add(new Student(3, "a", 2));
        set.stream().forEach(System.out::println);
        System.out.println("--------------------");
    }


}

class Student implements Comparable<Student> {
    private int sid;
    private String name;
    private int age;

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        if (this.sid < o.sid) {
            return -1;
        } else if (this.sid == o.sid) {
            if (this.age < o.age) {
                return -1;
            }
            return 1;
        }
        return 1;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

class MyStudentComparator implements Comparator<Student> {

    @Override
    public int compare(Student s1, Student s2) {
        if (s1.getSid() < s2.getSid()) {
            return -1;
        } else if (s1.getSid() == s2.getSid()) {
            if (s1.getAge() < s2.getAge()) {
                return -1;
            }
            return 1;
        }
        return 1;
    }
}


